//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ShipmentDetailsDlg.h"
#include "FormUtilities.h"
#include "ShipmentBridge.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

const int CODE_AMOUNT = 1;

//---------------------------------------------------------------------------
__fastcall TfrmShipmentDetailsDlg::TfrmShipmentDetailsDlg(TComponent* Owner)
        : TForm(Owner)
{
}


__fastcall TfrmShipmentDetailsDlg::TfrmShipmentDetailsDlg(TComponent* Owner, TDatabase* dbMain, TStrings * details)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->details = details;

   cmbFirms->Sorted = true;
   cmbModels->Sorted = true;

   bridge = new TGlassesBridge(dbMain);
   
   FillFirmsCombo();
   FillModelsCombo();
}

void TfrmShipmentDetailsDlg::FillFirmsCombo()
{
   bridge->GetFirms(cmbFirms->Items);

   if (cmbFirms->Items->Count > 0)
      cmbFirms->ItemIndex = 0;

}

void TfrmShipmentDetailsDlg::FillModelsCombo()
{
   if (cmbFirms->Items->Count > 0)
   {
      TGlassesFirm * firm = (TGlassesFirm*)cmbFirms->Items->Objects[cmbFirms->ItemIndex];
      bridge->GetModels(firm->id, cmbModels->Items);

      if (cmbModels->Items->Count > 0)
         cmbModels->ItemIndex = 0;
   }
}


void __fastcall TfrmShipmentDetailsDlg::cmbFirmsChange(TObject *Sender)
{
   FillModelsCombo();
}


//---------------------------------------------------------------------------

void __fastcall TfrmShipmentDetailsDlg::FormDestroy(TObject *Sender)
{
   delete bridge;        
}
//---------------------------------------------------------------------------


void __fastcall TfrmShipmentDetailsDlg::btnOkClick(TObject *Sender)
{
   try
   {
      ValidateData();
      
      TShipmentDetails * sd = new TShipmentDetails();

      TGlassesFirm * firm = (TGlassesFirm*)cmbFirms->Items->Objects[cmbFirms->ItemIndex];
      sd->firm = firm->name;
      sd->model = cmbModels->Items->Strings[cmbModels->ItemIndex];
      sd->amount = StrToInt64(edtAmount->Text);

      details->AddObject( IntToStr(sd->id), sd);
   }
   catch( TError * e)
   {
      HandleError( e);
   }//try-catch
}
//---------------------------------------------------------------------------

void TfrmShipmentDetailsDlg :: ValidateData()
{
   if ( edtAmount->Text.Length() == 0)
      throw new TError( CODE_AMOUNT, "Amount must be entered");

   int amount = 0;
   try
   {
      amount = StrToInt( edtAmount->Text);
   }
   catch( Exception * e)
   {
      throw new TError( CODE_AMOUNT, "Illegal amount number");
   }//try-catch

   if (amount <= 0)
      throw new TError( CODE_AMOUNT, "Amount number must be greater then zero");
}

void TfrmShipmentDetailsDlg :: HandleError( TError * e)
{
   MessageDlg( e->Message,
               mtError,
               TMsgDlgButtons() << mbOK,
               0);

   switch ( e->GetCode())
   {
      case CODE_AMOUNT :
         edtAmount->SetFocus();
         break;
   }//switch

   ModalResult = mrNone;
}

